package com.sdkx.app.activitys;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.NearestUseManager;
import com.sdkx.app.ViewModel.WayPointManager;
import com.sdkx.app.adapter.TabPagerAdapter;
import com.sdkx.app.fragment.NearestDistanceFragment;
import com.sdkx.app.fragment.NearestUseFragment;

import java.util.ArrayList;
import java.util.List;

public class NearestActivity extends AppCompatActivity {
    public double currentLongitude=180;
    public double currentLatitude=180;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 这句代码修复点击屏幕顶部不灵敏。
        getWindow().setFlags(1024, 1024);
        setContentView(R.layout.activity_nearest);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        Intent intent = getIntent();
        if(intent!=null){
            currentLatitude = intent.getDoubleExtra("latitude",180);
            currentLongitude = intent.getDoubleExtra("longitude",180);
        }

        TabLayout tabLayout = (TabLayout)findViewById(R.id.tablayout);
        ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);

        List<Fragment> fragmentList = new ArrayList<>();
        List<String> titleList = new ArrayList<>();
        fragmentList.add(new NearestUseFragment());
        fragmentList.add(new NearestDistanceFragment());
        titleList.add("最近使用的");
        titleList.add("最近距离的");
        viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager(), NearestActivity.this, fragmentList, titleList));
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        for(int i=0;i<titleList.size();i++){
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            TextView textView = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab_item_layout, tabLayout, false);
            textView.setText(titleList.get(i));
            tab.setCustomView(textView);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
            default:
                break;
        }
        return true;
    }
}
